package jehc.djshi.sys.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BaseBTreeGridEntity;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.sys.model.XtDataDictionary;
import jehc.djshi.sys.service.XtDataDictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Desc 数据字典
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtDataDictionary")
@Api(value = "数据字典API",tags = "数据字典API",description = "数据字典API")
public class XtDataDictionaryController extends BaseAction {

	@Resource
	private XtDataDictionaryService xtDataDictionaryService;

	/**
	 * 查询数据字典列表并分页
	 */
	@GetMapping(value="/list")
	@Auth(value = "/xtDataDictionary/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询数据字典列表并分页", notes="查询数据字典列表并分页")
	public BaseResult getXtDataDictionaryListByCondition(){
		List<BaseBTreeGridEntity> list = new ArrayList<BaseBTreeGridEntity>();
		Map<String, Object> condition = new HashMap<String, Object>();
		List<XtDataDictionary> xtDataDictionaryList = xtDataDictionaryService.getXtDataDictionaryListByCondition(condition);
		for(int i = 0; i < xtDataDictionaryList.size(); i++){
			XtDataDictionary xtDataDictionary = xtDataDictionaryList.get(i);
			BaseBTreeGridEntity BaseBTreeGridEntity = new BaseBTreeGridEntity();
			BaseBTreeGridEntity.setId(xtDataDictionary.getId());
			BaseBTreeGridEntity.setPid(xtDataDictionary.getParentId());
			BaseBTreeGridEntity.setText(xtDataDictionary.getName());
			BaseBTreeGridEntity.setContent(xtDataDictionary.getRemark());
//			BaseZTreeEntity.setIcon("../deng/images/icons/target.png");
			if(0==xtDataDictionary.getState()){
				BaseBTreeGridEntity.setTempObject("启用");
			}else{
				BaseBTreeGridEntity.setTempObject("禁用");
			}
			if(StringUtils.isEmpty(xtDataDictionary.getParentId()) || "0".equals(xtDataDictionary.getParentId())){
				BaseBTreeGridEntity.setIntegerappend("0");
			}else{
				BaseBTreeGridEntity.setIntegerappend("1");
			}
			list.add(BaseBTreeGridEntity);
		}
		return outStr(BaseBTreeGridEntity.buildTree(list));
	}

	/**
	 * 查询单个数据字典
	 * @param id
	 * @param request
	 */
	@ApiOperation(value="查询单个数据字典", notes="查询单个数据字典")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/xtDataDictionary/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<XtDataDictionary> getXtDataDictionaryById(@PathVariable("id") String id,HttpServletRequest request){
		XtDataDictionary xtDataDictionary = xtDataDictionaryService.getXtDataDictionaryById(id);
		return outDataStr(xtDataDictionary);
	}

	/**
	 * 查询全部数据字典
	 * @return
	 */
	@GetMapping(value="/lists")
	@Auth(value = "/xtDataDictionary/lists",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	@ApiOperation(value="查询全部数据字典", notes="查询全部数据字典")
	public BaseResult<List<XtDataDictionary>> getXtDataDictionaryById(){
		Map<String,Object> condition = new HashMap<>();
		List<XtDataDictionary> XtDataDictionaryList = xtDataDictionaryService.getXtDataDictionaryListAllByCondition(condition);
		return outDataStr(XtDataDictionaryList);
	}

	/**
	 * 添加
	 * @param xtDataDictionary
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个数据字典", notes="创建单个数据字典")
	public BaseResult addXtDataDictionary(@RequestBody XtDataDictionary xtDataDictionary){
		int i = 0;
		if(null != xtDataDictionary){
			xtDataDictionary.setId(toUUID());
			i=xtDataDictionaryService.addXtDataDictionary(xtDataDictionary);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param xtDataDictionary
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个数据字典", notes="编辑单个数据字典")
	public BaseResult updateXtDataDictionary(@RequestBody XtDataDictionary xtDataDictionary){
		int i = 0;
		if(null != xtDataDictionary){
			i=xtDataDictionaryService.updateXtDataDictionary(xtDataDictionary);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除数据字典", notes="删除数据字典")
	public BaseResult delXtDataDictionary(String id){
		int i = 0;
		if(StringUtils.isNotBlank(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=xtDataDictionaryService.delXtDataDictionary(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}

